from Func_List_Files import list_ncfiles
from Func_Create_File import create_file
from netCDF4 import Dataset
from wrf import getvar
import numpy as np
import math
import csv
import os




# Define settings of all simulations
#----------------------------------------------------------------------------------------------

Input_Dir  = '.../Hurricane_Case/'               # Provide the input directory   
Output_Dir = '.../Post_Processing/Intensity/'    # Provide the output directory   
HNS        = ['Florence']
HRS        = ['96']
GSS        = ['8km']
TMS        = ['NoTurb']
CLTS       = ['TSKM2', 'Default', 'TSKP2']

# Extract intensity timeseries
#----------------------------------------------------------------------------------------------

Idx = 0
for HN in HNS:
    for GS in GSS:
        for TM in TMS:
            clt_count = 0
            for CLT in CLTS:

                Hurricane_Setting = HN + '_' + HRS[Idx] + 'Hours_' + GS + '_' + TM + '_' + CLTS[clt_count]
                Input_Dir_1       = Input_Dir + Hurricane_Setting

                ncfiles, Intensity = [], []
                ncfiles = list_ncfiles (Input_Dir_1, ncfiles)
                for ncfile in ncfiles:
                    os.chdir(Input_Dir_1)
                    ncfile     = Dataset(ncfile)
                    U10_2D     = np.array(getvar(ncfile, "U10"))
                    V10_2D     = np.array(getvar(ncfile, "V10"))
                    U10_1D     = U10_2D.flatten()
                    V10_1D     = V10_2D.flatten()
                    WND_SPD_10 = U10_1D

                    for i in range (WND_SPD_10.size - 1):
                        WND_SPD_10[i] = math.sqrt((U10_1D[i]**2)+(V10_1D[i]**2))

                    WND_SPD_10_max = np.amax(WND_SPD_10)
                    Intensity.append(WND_SPD_10_max)

# Export the output
#----------------------------------------------------------------------------------------------
                os.chdir(Output_Dir)
                Path = os.getcwd()
                MyFile=open('%s.csv' %Hurricane_Setting,'w')
                MyFile.write (Hurricane_Setting + "\n")
                MyFile.write ("Intensity\n")
                for element in Intensity:
                    MyFile.write (str(element) + "\n")
                MyFile.close()
                clt_count += 1
    Idx = Idx + 1

